<template>
  <el-dialog
    v-el-drag-dialog
    :title="type === 'add' ? '新增车辆' : '编辑车辆'"
    :visible.sync="visible"
    width="700px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :rules="rules" :model="temp" label-position="right" label-width="80px">
      <el-form-item label="车牌号" prop="truckCode" required>
        <el-input v-model="temp.truckCode" :maxlength="20" />
      </el-form-item>
      <el-form-item label="所属单位" prop="orgSNNo">
        <el-tree-select
          v-model="temp.orgSNNo"
          :clearable="false"
          class="tree-select"
          :popover-width="360"
          :select-params="{
            clearable: true,
            placeholder: '请选择所属单位',
          }"
          :tree-params=" {
            'default-expand-all': true,
            'expand-on-click-node': false,
            'highlight-current': true,
            props:{
              value:'orgSNNo',
              label: 'orgName',
              disabled: 'disabled',
            },
            data: treeData
          }"
        />
      </el-form-item>
      <el-form-item label="车型" prop="truckType">
        <el-input v-model="temp.truckType" :maxlength="20" />
      </el-form-item>
      <el-form-item prop="remark" class="block">
        <label slot="label">备注</label>
        <el-input v-model="temp.remark" type="textarea" :rows="3" maxlength="250" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        关闭
      </el-button>
      <el-button :loading="$loadingPlugin.add || $loadingPlugin.edit" type="primary" @click="success">
        保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { vehicleAdd } from '@/api'
  import { mapGetters } from 'vuex'

  export default {
    name: 'add-vehicle',
    props: {
      treeData: {
        type: Array,
        required: true
      }
    },
    data() {
      const validateTruckCode = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入车牌号'))
        } else {
          if (value !== '') {
            const reg = new RegExp('^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$')
            if (!reg.test(value)) {
              callback(new Error('请输入有效的车牌号'))
            }
          } else {
            callback()
          }
          callback()
        }
      }
      return {
        visible: false,
        type: 'add',
        temp: {
          truckCode: '',
          orgSNNo: '',
          truckType: '',
          remark: ''
        },
        oldTruckCode: '',
        rules: {
          truckCode: [{ validator: validateTruckCode, trigger: 'blur' }],
          orgSNNo: [{ required: true, message: '请选择所属单位', trigger: 'change' }],
          truckType: [{ required: true, message: '请输入车型信息', trigger: 'change' }]
        }
      }
    },
    computed: {
      ...mapGetters([
        'orgList'
      ])
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.temp = Object.assign(temp)
            this.oldTruckCode = temp.truckCode
            this.type = 'edit'
          }
        })
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = { ...this.temp }
          const currentOrg = this.orgList.find(item => item.orgSNNo === this.temp.orgSNNo)
          params.orgId = currentOrg.orgId
          params.orgName = currentOrg.orgName
          if (this.type === 'add') {
            this.add(params)
          } else {
            this.edit(params)
          }
        })
      },
      async add(params) {
        await vehicleAdd(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '新增成功',
          type: 'success',
          duration: 2000
        })
      },
      async edit(params) {
        await vehicleAdd({
          ...params,
          oldTruckCode: this.oldTruckCode
        })
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '更新成功',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.type = 'add'
        this.temp = {
          truckCode: '',
          orgSNNo: '',
          truckType: '',
          remark: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  & /deep/ .el-form-item {
    width: 50%;
    padding-right: 10px;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
.info-title {
  display: flex;
  align-items: center;
  margin-top: 10px;
  margin-bottom: 20px;
  .title-icon {
    display: inline-block;
    width: 4px;
    height: 18px;
    margin-right: 6px;
    background: $themeColor;
  }
  span {
    font-weight: 700;
    color: $themeColor;
  }
}
</style>
